package com.lxmes.hr.service.impl;

import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.domain.NcWorkOvertime;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcWorkOverCalculateMapper;
import com.lxmes.hr.domain.NcWorkOverCalculate;
import com.lxmes.hr.service.INcWorkOverCalculateService;

import javax.validation.Validator;

/**
 * 加班计算规则Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-15
 */
@Service
public class NcWorkOverCalculateServiceImpl implements INcWorkOverCalculateService
{
    @Autowired
    private NcWorkOverCalculateMapper ncWorkOverCalculateMapper;

    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询加班计算规则
     *
     * @param id 加班计算规则主键
     * @return 加班计算规则
     */
    @Override
    public NcWorkOverCalculate selectNcWorkOverCalculateById(Long id)
    {
        return ncWorkOverCalculateMapper.selectNcWorkOverCalculateById(id);
    }

    /**
     * 查询加班计算规则列表
     *
     * @param ncWorkOverCalculate 加班计算规则
     * @return 加班计算规则
     */
    @Override
    public List<NcWorkOverCalculate> selectNcWorkOverCalculateList(NcWorkOverCalculate ncWorkOverCalculate)
    {
        return ncWorkOverCalculateMapper.selectNcWorkOverCalculateList(ncWorkOverCalculate);
    }

    /**
     * 新增加班计算规则
     *
     * @param ncWorkOverCalculate 加班计算规则
     * @return 结果
     */
    @Override
    public int insertNcWorkOverCalculate(NcWorkOverCalculate ncWorkOverCalculate)
    {
        ncWorkOverCalculate.setCreateBy(SecurityUtils.getUsername());
        ncWorkOverCalculate.setCreateTime(DateUtils.getNowDate());
        List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
        if(!StringUtils.isNull(ncOrgManages)){
            ncWorkOverCalculate.setGcCode(ncOrgManages.get(0).getJtdm());
            ncWorkOverCalculate.setGcName(ncOrgManages.get(0).getJtmc());
        }
        return ncWorkOverCalculateMapper.insertNcWorkOverCalculate(ncWorkOverCalculate);
    }

    /**
     * 修改加班计算规则
     *
     * @param ncWorkOverCalculate 加班计算规则
     * @return 结果
     */
    @Override
    public int updateNcWorkOverCalculate(NcWorkOverCalculate ncWorkOverCalculate)
    {
        ncWorkOverCalculate.setUpdateTime(DateUtils.getNowDate());
        ncWorkOverCalculate.setUpdateBy(SecurityUtils.getUsername());
        return ncWorkOverCalculateMapper.updateNcWorkOverCalculate(ncWorkOverCalculate);
    }

    /**
     * 批量删除加班计算规则
     *
     * @param ids 需要删除的加班计算规则主键
     * @return 结果
     */
    @Override
    public int deleteNcWorkOverCalculateByIds(Long[] ids)
    {
        return ncWorkOverCalculateMapper.deleteNcWorkOverCalculateByIds(ids);
    }

    /**
     * 删除加班计算规则信息
     *
     * @param id 加班计算规则主键
     * @return 结果
     */
    @Override
    public int deleteNcWorkOverCalculateById(Long id)
    {
        return ncWorkOverCalculateMapper.deleteNcWorkOverCalculateById(id);
    }

    @Override
    public String imporIteminfo(List<NcWorkOverCalculate> iteminfoList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(iteminfoList) || iteminfoList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcWorkOverCalculate iteminfo : iteminfoList)
        {
            try
            {
                BeanValidators.validateWithException(validator, iteminfo);
                iteminfo.setCreateBy(operName);
                iteminfo.setUpdateBy(operName);
                this.insertNcWorkOverCalculate(iteminfo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、标准 " + iteminfo.getEmployeeName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + iteminfo.getEmployeeName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
